package com.drinks.yiniang.adapter.community;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.banner.CommunityBannerRecyclerAdapter;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.community.CommunityNewsJson;
import com.drinks.yiniang.ui.widget.layout.ArrowExpand2Layout;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.banner.recycler.RecyclerBanner2Layout;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户社区，分享列表adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class UCShareAdapter extends BaseQuickRCVAdapter<CommunityNewsJson.CommunityNewsList, BaseViewHolder> {

    private AppCompatTextView mTextView;
    private ArrowExpand2Layout mArrowExpandLayout;

    private ViewGroup.LayoutParams params;

    private Drawable mTagDrawable;
    private Drawable mCollectNormalDrawable;
    private Drawable mCollectFocusDrawable;
    private Drawable mLikeNormalDrawable;
    private Drawable mLikeFocusDrawable;

    private CommunityBannerRecyclerAdapter mBannerAdapter;

    private SpannableStringBuilder mBuilder;

    private String[] mTagArray;

    private int dp8, dp3;
    private int start, end;

    public UCShareAdapter(Context mContext, @Nullable List<CommunityNewsJson.CommunityNewsList> data) {
        super(R.layout.item_uc_share, data);

        mTagDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_tag);

        mCollectNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_n);
        mCollectFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_p);

        mLikeNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_n);
        mLikeFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_p);

        dp8 = ScreenSizeUtils.dp2px(mContext, 8);
        dp3 = ScreenSizeUtils.dp2px(mContext, 3);

        params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ScreenSizeUtils.dp2px(mContext, 22));

        mBuilder = new SpannableStringBuilder();
    }

    @Override
    protected void convert(BaseViewHolder helper, CommunityNewsJson.CommunityNewsList item) {
        if (item.getFans() == null) {
            item.setFans(new CommunityNewsJson.CommunityNewsUser());
        }

        //头像
        GlideUtils.loadAvatar(mContext, (CircleImageView) helper.getView(R.id.iv_uc_share_avatar), item.getFans().getAvatar());
        //banner
        setBannerData(helper, item);
        //昵称
        helper.setText(R.id.tv_uc_share_nickname, item.getFans().getUsername());
        //标题
        helper.setText(R.id.tv_uc_share_title, item.getTitle());
        //标签
        addLabel((AutoLineFeedLayout) helper.getView(R.id.auto_feed_uc_share_label), "");
        //内容
        setContent(helper, item);
        //收藏数点赞数
        setCollectAndLikeData((TextView) helper.getView(R.id.tv_uc_share_collect), (TextView) helper.getView(R.id.tv_uc_share_like), item);
        //评论数
        helper.setText(R.id.tv_uc_share_total_comments, String.format(mContext.getString(R.string.text_total_comment_format), StringUtils.getNullEmptyConvertZero(item.getComment_count())));
        //评论
        getCommentView((LinearLayout) helper.getView(R.id.ll_uc_share_comments), item.getComment());
        //时间
        helper.setText(R.id.tv_uc_share_time, "这是时间，目前没返回");

        helper.addOnClickListener(R.id.iv_uc_share_three_dot);
        helper.addOnClickListener(R.id.tv_uc_share_collect, R.id.tv_uc_share_like);
        helper.addOnClickListener(R.id.tv_uc_share_total_comments, R.id.tv_uc_share_comments_add);
    }

    private void setBannerData(BaseViewHolder helper, CommunityNewsJson.CommunityNewsList item) {
        if(item.getImages() == null) {
            item.setImages(new ArrayList<CommunityNewsJson.CommunityNewsImg>());
        }
        RecyclerBanner2Layout banner = helper.getView(R.id.ll_uc_share_banner);

        mBannerAdapter = new CommunityBannerRecyclerAdapter(mContext);
        mBannerAdapter.addBannerData(item.getImages());
        banner.setBannerAdapter(mBannerAdapter);
        banner.start();
    }

    //添加标签
    private void addLabel(AutoLineFeedLayout label, String tagStr) {
        label.removeAllViews();

        if (StringUtils.isEmpty(tagStr)) {
            return;
        }

        mTagArray = tagStr.split(",");

        if (mTagArray == null || mTagArray.length == 0) {
            return;
        }

        for (String tag : mTagArray) {
            mTextView = new AppCompatTextView(mContext);
            mTextView.setPadding(dp8, 0, dp8, 0);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            mTextView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_3d6bd8));
            mTextView.setGravity(Gravity.CENTER_VERTICAL);
            mTextView.setCompoundDrawablesWithIntrinsicBounds(mTagDrawable, null, null, null);
            mTextView.setBackgroundResource(R.drawable.shape_bkg_follow_label);
            mTextView.setCompoundDrawablePadding(dp3);
            mTextView.setText(StringUtils.getNullEmptyConvert__(tag));
        }

        label.addView(mTextView, params);
    }

    //设置内容
    private void setContent(final BaseViewHolder helper, final CommunityNewsJson.CommunityNewsList item) {
        mArrowExpandLayout = helper.getView(R.id.ll_uc_share_content);
        mArrowExpandLayout.removeAllViews();
        mArrowExpandLayout.setExpand(item.isContentExpand());
        mArrowExpandLayout.addExpandTextView(item.getContent(), item.isContentExpand() ? Integer.MAX_VALUE : 2);

        mArrowExpandLayout.setOnArrowExpandChangeListener(new ArrowExpand2Layout.onArrowExpandChangeListener() {
            @Override
            public void onArrowExpandChange(boolean isExpand) {
                item.setContentExpand(isExpand);
                setData(helper.getAdapterPosition(), item);
            }
        });
    }

    private void setCollectAndLikeData(TextView tvCollect, TextView tvLike, CommunityNewsJson.CommunityNewsList item) {
        //收藏数
        tvCollect.setText(StringUtils.getNullEmptyConvertZero(item.getCollect_count()));
        tvCollect.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_collect()) ? mCollectFocusDrawable : mCollectNormalDrawable, null, null, null);
        //点赞数
        tvLike.setText(StringUtils.getNullEmptyConvertZero(item.getLike_count()));
        tvLike.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_like()) ? mLikeFocusDrawable : mLikeNormalDrawable, null, null, null);
    }

    private void getCommentView(LinearLayout ll, List<CommunityNewsJson.CommunityNewsComment> comments) {
        ll.removeAllViews();

        if (CommonUtils.isEmptyList(comments)) {
            return;
        }

        for (int i = 0; i < comments.size(); i++) {
            mBuilder.clearSpans();
            mBuilder.clear();
            mBuilder.append(StringUtils.getNullEmptyConvert__(comments.get(i).getNickname()));
            end = mBuilder.length();
            mBuilder.append(" ");
            mBuilder.append(StringUtils.getNullEmptyConvert__(comments.get(i).getContent()));
            mBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            mTextView = new AppCompatTextView(mContext);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            mTextView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_282829));
            mTextView.setLines(1);
            mTextView.setEllipsize(TextUtils.TruncateAt.END);
            mTextView.setText(mBuilder);

            ll.addView(mTextView);
        }
    }
}
